/**
 * Created by Wang on 2018/4/10.
 */
layui.use(['costshareOne'], function(){

    var costId = GetQueryString("costId");
    var catalogId = GetQueryString("catalogId");

    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }

    $("#prevBtn").on('click',function (){
        window.location.href = "/ceca_two/modules/share/civil/three.html"+window.location.search;
    });

    $("#nextBtn").on("click",function (){

        var data = saveData();

        $.ajax({
            type:"post",
            url:"/guidance_service/front/costIndex/update.do",
            data:{
                costId : costId,
                perSquareQuantity: data
            },
            success:function (result){
                console.log(result)
                window.location.href = "/ceca_two/modules/share/shareSubmit.html"+window.location.search;
            },error:function (){
                layer.msg("请求错误！请稍候重试",{icon:2,time:3000});
            }
        });
    })
});


setInterval(function (){
    sessionStorage.setItem("civilFour", saveData());
},30000);

if (sessionStorage.getItem('civilFour')) {
    var data = JSON.parse(sessionStorage.getItem('civilFour'));

    for (i = 0 ; i < data.length ; i++) {
        $(".shareTable").find("tr").eq(i+1).find("input").eq(0).val(data[i].dosage);
        $(".shareTable").find("tr").eq(i+1).find("input").eq(1).val(data[i].money);
    }

}



function saveData(){
    var arr = [];

    for (i = 1 ; i < $(".shareTable tr").length ; i++) {

        arr.push({
            name : $(".shareTable tr").eq(i).find("td").eq(1).html(),
            unit : $(".shareTable tr").eq(i).find("td").eq(2).html(),
            dosage : $(".shareTable tr").eq(i).find("td").eq(3).find("input").val(),
            money : $(".shareTable tr").eq(i).find("td").eq(4).find("input").val()

        })
    }

    return	arr = JSON.stringify(arr);
}

//敲击按键时触发
    $("input").bind("keypress", function(event) {
        var event= event || window.event;
        var getValue = $(this).val();
        //控制第一个不能输入小数点"."
        if (getValue.length == 0 && event.which == 46) {
            event.preventDefault();
            return false;
        }
        //控制只能输入一个小数点"."
        if (getValue.indexOf('.') != -1 && event.which == 46) {
            event.preventDefault();
            return false;
        }
        //控制只能输入的值
        if (event.which && (event.which < 48 || event.which > 57) && event.which != 8 && event.which != 46) {
            event.preventDefault();
            return false;
        }
    });
//失去焦点是触发
$("#Score").bind("blur", function(event) {
    var value = $(this).val();
    var reg = /\.$/;

    if (reg.test(value)) {
        value = value.replace(reg, "");
        $(this).val(value);
    }
});